import Vue from 'vue'
import VueRouter from 'vue-router'
// @ 是 src 目录的别名
import Home from '../views/Home.vue'
import Person from '../views/Person.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import NewsList from '@/views/NewsList.vue'
import NewsInfo from '@/views/NewsInfo.vue'
import About from '@/views/About.vue'
import Cart from '@/views/Cart.vue'
import GoodDetail from '@/views/GoodDetail.vue'
import MyCart from '@/views/MyCart.vue'
// import MyHeader from '@/views/MyHeader.vue'
// import Left from '@/views/Left.vue'
// import Right from '@/views/Right.vue'


Vue.use(VueRouter)



let routes = [
    {
        path:'/',
        redirect:'/home'
        // components:{
        //     default:MyHeader,
        //     l:Left,
        //     r:Right
        // }
    },
    {
        path:'/newslist',
        name:'NewsList',
        component:NewsList
    },
    {
        path:'/home',
        alias:'/hh',
        component:Home,
        meta:{
            show:true
        },
        children:[
            {
                path:'login',
                component:Login
            },
            {
                path:'register',
                component:Register
            }
        ]
    },
    {
        path:'/person',
        name:'Person',
        component:Person
    },
    {
        path:'/newsinfo/:id',
        name:'NewsInfo',
        component:NewsInfo,
        beforeEnter: (to, from, next) => {
            console.group('beforeEnter')
            console.log(to);
            console.log(from);
            console.groupEnd()
            next()
        }
    }
    // {
    //     path:'/newsinfo',
    //     name:'NewsInfo',
    //     component:NewsInfo
    // }
    //  ,{
    //      path:"*"
    //  }
    ,
    {
        path:'/about',
        name:"About",
        component:About
    },
    {
        path:'/cart',
        name:'Cart',
        component:Cart,
        // 元信息
        meta:{
            show:true
        }
    },
    {
        path:'/gooddetail/:id',
        name:'GoodDetail',
        component:GoodDetail

    },
    {
        path:'/mycart',
        name:'MyCart',
        component:MyCart
    }

    
]
const router = new VueRouter({
    routes
})

// 全局 前置守卫
router.beforeEach((to,from,next)=>{
    console.group('beforeEach')
    console.log(to);
    console.log(from);
    console.groupEnd()
    // if(to.path=="/ddd")

    // if(to.path == ''){
    //     next()
    // }else if(xxx){
    //     next('/newslist')
    // }else{
    //     next(new Error('xxx'))
    // }

    next()
    // next(false)
    // next('/')
    // next(new Error('发生错误了'))
})

router.afterEach((to,from)=>{
    console.group('afterEach')
    console.log(to);
    console.log(from);
    console.groupEnd()
})

router.onError((err)=>{
    console.log(err);
})


export default router


